package com.team.lostProperty.controller;

import com.alibaba.fastjson.JSON;
import com.team.lostProperty.entity.Collect;
import com.team.lostProperty.entity.PushInfo;
import com.team.lostProperty.entity.Result;
import com.team.lostProperty.service.CollectService;
import com.team.lostProperty.service.PushInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duzq
 * @date 2020/7/25 19:02
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private PushInfoService pushInfoService;

    Result result = new Result();

    /**
     * 根据用户查询收藏
     * @param userId
     * @param start
     * @return String
     */
    @CrossOrigin
    @RequestMapping("/getCollect/{userId}/{start}")
    private String getCollect(@PathVariable("userId") long userId,@PathVariable("start") int start){
        start = start * 10;
        if (start >= collectService.getAllCount(userId)){
            result.setCode(0);
            result.setMsg("成功");
            result.setCount(0);
            result.setData("noMore");
            return JSON.toJSONString(result);
        }
        List<Collect> collectList = collectService.getCollectById(userId,start);
        if(collectList.size()>0){
            List<PushInfo> pushInfoList = new ArrayList();
            for (int i=0; i<collectList.size(); i++){
                Collect collect = collectList.get(i);
                PushInfo pushInfo = pushInfoService.getPushInfoById(collect.getPushInfo().getPushInfoId());
                pushInfoList.add(pushInfo);
            }
            result.setCode(0);
            result.setMsg("成功");
            result.setCount(collectService.getAllCount(userId));
            result.setData(pushInfoList);
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setCount(0);
            result.setData("");
        }
        return JSON.toJSONString(result);
    }

    /**
     * 根据用户查询收藏
     * @param userId
     * @param page
     * @param limit
     * @return String
     */
    @CrossOrigin
    @RequestMapping("/getCollectByUser")
    private String getCollectByUser(String userId,int page,int limit){
        List<Collect> collectList = collectService.getCollectByUser(Long.valueOf(userId),page,limit);
        if(collectList.size()>0){
            List<PushInfo> pushInfoList = new ArrayList();
            for (int i=0; i<collectList.size(); i++){
                Collect collect = collectList.get(i);
                PushInfo pushInfo = pushInfoService.getPushInfoById(collect.getPushInfo().getPushInfoId());
                pushInfoList.add(pushInfo);
            }
            result.setCode(0);
            result.setMsg("成功");
            result.setCount(collectService.getAllCount(Long.valueOf(userId)));
            result.setData(pushInfoList);
        }else{
            result.setCode(1);
            result.setMsg("失败");
            result.setCount(0);
            result.setData("");
        }
        return JSON.toJSONString(result);
    }
}
